package com.zmn.oms.services.impl.es;

import com.zmn.base.oms.common.consts.OrderElasticConsts;
import com.zmn.base.oms.common.model.es.orderpay.EsOrderPay;
import com.zmn.framework.biz.esearch.model.EsBaseQuery;
import com.zmn.framework.biz.esearch.service.impl.ESearchServiceImpl;
import com.zmn.oms.services.interfaces.es.OrderPayESearchService;
import org.elasticsearch.search.aggregations.Aggregations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 类描述：
 *
 * @author wangxiaokun
 * @since 2022/05/16 18:49
 */
@Service
public class OrderPayESearchServiceImpl extends ESearchServiceImpl<EsBaseQuery, EsOrderPay, Long> implements OrderPayESearchService {

    @Override
    protected String getIndexName() {
        return OrderElasticConsts.ORDER_PAY_INDEX;
    }

    @Override
    protected Class<EsOrderPay> getDOClass() {
        return EsOrderPay.class;
    }

    @Override
    public Aggregations getAggregationsByQuery(NativeSearchQuery nativeSearchQuery) {
        SearchHits<EsOrderPay> search = getElasticsearchRestTemplate().search(nativeSearchQuery, EsOrderPay.class);
        nativeSearchQuery.setMaxResults((int) search.getTotalHits());
        return search.getAggregations();
    }

    @Override
    public List<EsOrderPay> listPageByQuery(NativeSearchQuery nativeSearchQuery) {
        SearchHits<EsOrderPay> search = getElasticsearchRestTemplate().search(nativeSearchQuery, EsOrderPay.class);
        nativeSearchQuery.setMaxResults((int) search.getTotalHits());
        return search.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
    }

    @Override
    public Long countByQuery(NativeSearchQuery nativeSearchQuery) {
        return getElasticsearchRestTemplate().count(nativeSearchQuery, EsOrderPay.class);
    }
}
